package tsing.javase.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class Test5 {


    public static void main(String[] args) {

        try {
            Scheduler scheduler  =new StdSchedulerFactory().getScheduler();
            // 描述JobDetail 实例，给一个标识符
            JobKey jobKey1 = JobKey.jobKey("job1","group1");
            // 创建第一个JobDetail实例
            JobDetail jobDetail1 = JobBuilder.newJob(TestJob.class)
                    .withIdentity(jobKey1)
                    .usingJobData("name", "job1")
                    .build();

            JobKey jobKey2 = JobKey.jobKey("job2","group2");
            // 创建第二个JobDetail实例
            JobDetail jobDetail2 = JobBuilder.newJob(TestJob.class)
                    .withIdentity(jobKey2)
                    .usingJobData("name", "job2")
                    .build();

            // 设置不同的cron表达式
            Trigger trigger1 = TriggerBuilder.newTrigger()
                    .withIdentity("trigger1", "group1")
                    .withSchedule(CronScheduleBuilder.cronSchedule("0/10 * * * * ? ")) // 每天中午12点执行
                    .build();

            Trigger trigger2 = TriggerBuilder.newTrigger()
                    .withIdentity("trigger2", "group2")
                    .withSchedule(CronScheduleBuilder.cronSchedule("0/10 * * * * ? ")) // 每天下午6点执行
                    .build();

            // 将JobDetail和Trigger关联起来
            scheduler.scheduleJob(jobDetail1, trigger1);
            scheduler.scheduleJob(jobDetail2, trigger2);


            // 启动Scheduler
            scheduler.start();
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 获取调度器中的任务实例
            JobDetail jobDetail = scheduler.getJobDetail(jobKey1);
            // 删除job
            JobKey jobKey = new JobKey("111", "222");
            scheduler.deleteJob(jobKey1);
            scheduler.deleteJob(jobKey);

            System.out.println("isDurable:"+jobDetail.isDurable());

        } catch (SchedulerException e) {
            e.printStackTrace();
            System.out.println(e);
        }


    }
}
